package de.pmg_grandma;

import interfaces.AbstractDrink;
import interfaces.AbstractFood;
import interfaces.AbstractWish;

import java.util.ArrayList;

import wishes.WishToDrink;
import wishes.WishToEat;
import model.Model;
import model.WishFactory;
import enums.WishType;
import food.BockwurstMitKartoffeln;
import food.ErbsenEintopf;
import food.Griessbrei;
import food.HaferflockenMitMilch;
import food.Cheesburger;
import food.Salat;
import food.Sandwich;
import food.Schnitzel;
import food.Steak;
import de.pmg_grandma.R.drawable;
import de.pmg_grandma.SimpleGestureFilter;
import de.pmg_grandma.SimpleGestureFilter.SimpleGestureListener;
import drinks.Water;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class KitchenActivity extends Activity implements SimpleGestureListener{

	private SimpleGestureFilter detector;
	private ArrayList<Integer> menueImg = new ArrayList<Integer>();

	private ImageView foodImage1;
	private ImageView foodImage2;
	private ImageView foodImage3;
	private ImageView waterImage;

	private int foodCounter = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.i("Activity", "KitchenActivity onCreate()");
		
		//Markiere, dass App den Fokus hat
		Model.getInstance().inAppFocus = true;
		setContentView(R.layout.activity_kitchen);

		//Listner, zum erkennen des Swipes
		detector = new SimpleGestureFilter(this, this);

		
		//Liste mit ImageId's der jeweiligen Gerichte
		menueImg.add(R.drawable.hamburger_normal);
		menueImg.add(R.drawable.erbseneintopf_normal);
		menueImg.add(R.drawable.sandwich_normal);
		menueImg.add(R.drawable.schnitzel_normal);
		menueImg.add(R.drawable.bockwurst_normal);
		menueImg.add(R.drawable.haferflocken_normal);
		menueImg.add(R.drawable.steak_normal);
		menueImg.add(R.drawable.griessbrei_normal);
		menueImg.add(R.drawable.salat_normal);

		
		//Initialisierung der ersten drei Bilder
		foodImage1 = (ImageView) findViewById(R.id.img_kitchen_food1);
		foodImage1.setImageResource(iWillUseThisId(menueImg.get(foodCounter)));
		foodImage1.setTag(iWillUseThisId(menueImg.get(foodCounter++)));

		foodImage2 = (ImageView) findViewById(R.id.img_kitchen_food2);
		foodImage2.setImageResource(iWillUseThisId(menueImg.get(foodCounter)));
		foodImage2.setTag(iWillUseThisId(menueImg.get(foodCounter++)));

		foodImage3 = (ImageView) findViewById(R.id.img_kitchen_food3);
		foodImage3.setImageResource(iWillUseThisId(menueImg.get(foodCounter)));
		foodImage3.setTag(iWillUseThisId(menueImg.get(foodCounter++)));

		waterImage = (ImageView) findViewById(R.id.img_kitchen_water_normal);
		Water water = new Water();
		
		//Wenn nicht genügend geschirr vorhanden ist ODER ist nicht genug Wasser mehr vorhanden --> Graue Bild aus
		if(!water.hasEnoughDishes() || !water.hasEnoughIngredients())
			waterImage.setImageResource(drawable.water_disable);

		AbstractWish wish = Model.getInstance().getWishes().get(WishType.WishToEat);
		if(wish != null){
			
			//Ist nicht genug Geschirr vorhanden --> Generiere Abwaschwunsch
			if(!((WishToEat) wish).getFood().hasEnoughDishes())
				WishFactory.createWishToWashDishes();
			
			//Sind nicht genug Lebensmittel für Speise vorhanden --> Geniere Einkaufenswunsch
			if(!((WishToEat) wish).getFood().hasEnoughIngredients())
				WishFactory.createWishToShop();
		}
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent me) {
		this.detector.onTouchEvent(me);
		return super.dispatchTouchEvent(me);
	}

	public void onButtonClick(View view) {
		this.finish();
	}

	@Override
	public void onSwipe(int direction) {
		switch (direction) {
		case SimpleGestureFilter.SWIPE_DOWN :   
			rollDown("down");
			break;
		case SimpleGestureFilter.SWIPE_UP :    
			rollDown("up");
			break;
		}
	}

	//Scrollen des Essensmenüs
	public void rollDown(String orientation) {	
		//Tag zwischen speichern
		int tempID = 0;
		
		//wenn wir nach oben swipen
		//zähle den Counter erstmal 3 runter für die derzeitig angezeigten Bilder
		//und dann nochmal drei, damit man bei dem Index ist, dessen Bild als erstes
		//angezegt werden soll
		if(orientation.equals("up")) {
			for(int i = 0; i < 6; i++)
				updateFoodCounter("up");
		}
		Log.i("Blobs", "Rueckgabe: " + menueCanCreate(menueImg.get(foodCounter)));
		
		//prüfe die ID, die man nutzen möchte und graue eventuell ab
		tempID = iWillUseThisId(menueImg.get(foodCounter));
		//Update der Bilder und Counter
		foodImage1.setTag(tempID);
		foodImage1.setImageResource(tempID);
		updateFoodCounter("down");

		tempID = iWillUseThisId(menueImg.get(foodCounter));
		foodImage2.setTag(tempID);
		foodImage2.setImageResource(tempID);
		updateFoodCounter("down");

		tempID = iWillUseThisId(menueImg.get(foodCounter));
		foodImage3.setTag(tempID);
		foodImage3.setImageResource(tempID);
		updateFoodCounter("down");
	}
	
	
	//Funktion um die Bild-ID richtig zu berechnen
	public void updateFoodCounter(String orientation) {
		if(orientation.equals("down"))
			foodCounter++;
		else if(orientation.equals("up"))
			foodCounter--;

		if(foodCounter > 8)
			foodCounter = 0;
		else if(foodCounter < 0)
			foodCounter = 8;
	}

	public void foodClick(View view) {
		String anzeigeText = "" + view.getTag();
		AbstractFood food = null;

		//Reagiere auf das jewillige, gecklickte bild
		if(view.getTag().equals(R.drawable.bockwurst_normal)) {
			food = new BockwurstMitKartoffeln();
			anzeigeText = Model.SPEISE_BOCKWURST_MIT_KARTOFFELN;
		} 
		else if(view.getTag().equals(R.drawable.erbseneintopf_normal)) {
			food = new ErbsenEintopf();
			anzeigeText = Model.SPEISE_ERBSENEINTOPF;
		} 
		else if(view.getTag().equals(R.drawable.griessbrei_normal)) {
			food = new Griessbrei();
			anzeigeText = Model.SPEISE_GRIESSBREI;
		} 
		else if(view.getTag().equals(R.drawable.haferflocken_normal)) {
			food = new HaferflockenMitMilch();
			anzeigeText = Model.SPEISE_HAFERFLOCKEN_MIT_MILCH;
		} 
		else if(view.getTag().equals(R.drawable.hamburger_normal)) {
			food = new Cheesburger();
			anzeigeText = Model.SPEISE_CHEESBURGER;
		} 
		else if(view.getTag().equals(R.drawable.salat_normal)) {
			food = new Salat();
			anzeigeText = Model.SPEISE_SALAT;
		}		
		else if(view.getTag().equals(R.drawable.sandwich_normal)) {
			food = new Sandwich();
			anzeigeText = Model.SPEISE_SANDWICH;
		}		
		else if(view.getTag().equals(R.drawable.schnitzel_normal)) {
			food = new Schnitzel();
			anzeigeText = Model.SPEISE_SCHNITZEL;
		}
		else if(view.getTag().equals(R.drawable.steak_normal)) {
			food = new Steak();
			anzeigeText = Model.SPEISE_STEAK;
		}
		//auf disabled Bild geklicked: Gib Default-Ton
		else
			MediaPlayer.create(this, R.raw.disabled).start(); 

		if(food != null) {
			AbstractWish wish = Model.getInstance().getWishes().get(WishType.WishToEat);
			
			//Wenn Wunsch existiert UND Essenswunsch --> erfülle Wunsch und generiere Toast
			if(wish != null && ((WishToEat) wish).getFood().getType().equals(food.getType()) ){
				if(wish.fulfill()){
					Toast.makeText(
							KitchenActivity.this, 
							"Wunsch erfüllt (" + Model.getInstance().getWishes().size() + " Wünsche übrig)", 
							Toast.LENGTH_SHORT)
							.show();
				} else {
					Toast.makeText(
							KitchenActivity.this, 
							"Konnte Wunsch nicht erfüllen", 
							Toast.LENGTH_SHORT)
							.show();
				}
			}else{
				//kein Wunsch, einfach so essen!
				AbstractWish tempWish = new WishToEat(0, food);
				if(tempWish.canBeFulfilled()){
					Model.getInstance().eat(food);
					Toast.makeText(
							KitchenActivity.this, 
							food.getName() + " gegessen", 
							Toast.LENGTH_SHORT)
							.show();
				}
			}
			//Schließe Act
			finish();
		}
	}

	public void waterClick(View view) {
		AbstractDrink drink;
		AbstractWish wish = Model.getInstance().getWishes().get(WishType.WishToDrink);
		
		//Wenn Wunsch existiert
		if(wish != null) {
			
			//Erfülle Trinkwunsch und generiere Toast
			if(wish.fulfill()){
				Toast.makeText(
						KitchenActivity.this, 
						"Wunsch erfüllt (" + Model.getInstance().getWishes().size() + " Wünsche übrig)", 
						Toast.LENGTH_SHORT)
						.show();
			} else {
				Toast.makeText(
						KitchenActivity.this, 
						"konnte Wunsch nicht erfüllen", 
						Toast.LENGTH_SHORT)
						.show();
			}
		}else{
			//kein Wunsch
			drink = new Water();
			AbstractWish tempWish = new WishToDrink(0, drink);
			if(tempWish.canBeFulfilled()){
				Model.getInstance().drink(drink);
				Toast.makeText(
						KitchenActivity.this, 
						drink.getName() + " getrunken", 
						Toast.LENGTH_SHORT)
						.show();
			}
		}
		//Activity finish
		finish();
	}

	public boolean menueCanCreate (int menue) {
		boolean result = true;
		AbstractFood food;

		//Pruefe fuer jede Speisse, ob erforderlichen Lebensmittel vorhanden. Jede Speisse erfordert eine Einheit vom Lebensmittel
		switch(menue) {
		case(R.drawable.bockwurst_normal):
			food = new BockwurstMitKartoffeln();
		
		//Ergebnis der Pruefung, ob genug Geschirr vorhanden UND Genug Einheiten für Speise
		result = food.hasEnoughDishes() && food.hasEnoughIngredients(); 
		break;

		case(R.drawable.erbseneintopf_normal):
			food = new ErbsenEintopf();
		result = food.hasEnoughDishes() && food.hasEnoughIngredients(); 
		break;

		case(R.drawable.griessbrei_normal): 
			food = new Griessbrei();
		result = food.hasEnoughDishes() && food.hasEnoughIngredients();
		break;

		case(R.drawable.haferflocken_normal): 
			food = new HaferflockenMitMilch();
		result = food.hasEnoughDishes() && food.hasEnoughIngredients();
		break;
		case(R.drawable.hamburger_normal): 
			food = new Cheesburger(); 
		result = food.hasEnoughDishes() && food.hasEnoughIngredients();
		break;

		case(R.drawable.salat_normal): 
			food = new Salat();
		result = food.hasEnoughDishes() && food.hasEnoughIngredients();
		break;

		case(R.drawable.sandwich_normal):
			food = new Sandwich();
		result = food.hasEnoughDishes() && food.hasEnoughIngredients();
		break;

		case(R.drawable.schnitzel_normal):
			food = new Schnitzel();
		result = food.hasEnoughDishes() && food.hasEnoughIngredients();
		break;

		case(R.drawable.steak_normal):
			food = new Steak();
		result = food.hasEnoughDishes() && food.hasEnoughIngredients();
		break;
		}
		return result;
	}

	public int disableFood(int id) {
		
		//Erkenne die jeweillige Speise und setze das drawable als disabled (ausgrauen)
		switch(id) {
		
		case R.drawable.bockwurst_normal: 
			id = R.drawable.bockwurst_disable; break;

		case R.drawable.erbseneintopf_normal: 
			id = R.drawable.erbseneintopf_disable; break;

		case R.drawable.griessbrei_normal: 
			id = R.drawable.griessbrei_disable; break;

		case R.drawable.hamburger_normal: 
			id = R.drawable.hamburger_disable; break;

		case R.drawable.haferflocken_normal: 
			id = R.drawable.haferflocken_disable; break;

		case R.drawable.salat_normal: 
			id = R.drawable.salat_disable; break;

		case R.drawable.sandwich_normal: 
			id = R.drawable.sandwich_disable; break;

		case R.drawable.schnitzel_normal: 
			id = R.drawable.schnitzel_disable; break;

		case R.drawable.steak_normal: 
			id = R.drawable.steak_disable; break;
		}

		return id;
	}

	public int iWillUseThisId(int id){
		int returnID = id;
		if(!menueCanCreate(id))
			returnID = disableFood(id);
		return returnID;
	}

	@Override
	public void onDoubleTap() {
	}
	@Override
	protected void onPause() {
		Log.i("Activity", "KitchenActivity onPause()");
		Model.getInstance().inAppFocus = false;
		super.onPause();
	}
	@Override
	protected void onResume() {
		Model.getInstance().inAppFocus = true;
		super.onResume();
	}
	@Override
	protected void onDestroy() {
		Log.i("Activity", "KitchenActivity onDestroy()");
		super.onDestroy();
	}
}
